package com.ebei.admin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ebei.admin.mapper.SysRoleMenuMapper;
import com.ebei.admin.model.entity.SysRole;
import com.ebei.admin.model.entity.SysRoleMenu;
import com.ebei.admin.service.SysRoleMenuService;
import com.ebei.admin.service.SysRoleService;
import com.ebei.common.constant.CommonConstant;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 角色-菜单关联服务实现类
 *
 * @author crazypenguin
 * @version 1.0
 * @created 2018/4/17
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Boolean roleMenuUpd(Map<String, Object> params) {

        String roleCode = String.valueOf(params.get("roleCode"));

        // 查询角色
        SysRole conditionRole = new SysRole();
        conditionRole.setRoleCode(roleCode);
        conditionRole.setDelFlag(CommonConstant.STATUS_NORMAL);
        SysRole sysRole = sysRoleService.selectOne(new EntityWrapper<>(conditionRole));
        // 解除原先关联关系
        SysRoleMenu condition = new SysRoleMenu();
        condition.setRoleId(sysRole.getRoleId());
        this.delete(new EntityWrapper<>(condition));

        // 建立新的关联关系
        List<String> menuIds = new ArrayList<>();

        JSONArray tempArray = JSONArray.parseArray(params.get("roleAuth").toString());
        if(tempArray != null && tempArray.size() > 0){
            for(Object obj : tempArray){
                JSONArray roleAuth = JSONArray.parseArray(obj.toString());
                menuIds = getCheckedIds(roleAuth,menuIds);
            }
        }

        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        SysRoleMenu sysRoleMenu;
        for(String menuId : menuIds){
            sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenu.setMenuId(Integer.valueOf(menuId));
            sysRoleMenus.add(sysRoleMenu);
        }
        return this.insertBatch(sysRoleMenus);
    }

    private List<String> getCheckedIds(JSONArray roleAuth,List<String> menuIds){
        for(int i = 0; i < roleAuth.size(); i++){
            JSONObject menuJson = JSONObject.parseObject(roleAuth.get(i).toString());
            // 递归json
            if((Boolean)menuJson.get("checked")){
                menuIds.add(menuJson.get("id").toString());
            }
            if(menuJson.get("children") != null){
                JSONArray children = JSONArray.parseArray(menuJson.get("children").toString());
                if(children.size() > 0){
                    getCheckedIds(children,menuIds);
                }
            }
        }
        return menuIds;
    }

}
